Skip to content

MCP server: an HTTP mode for Home Assistant - #352

Open
vpatel9202 wants to merge 6 commits into
Seagey:mainfrom
vpatel9202:ha-mcp-http
Open

vpatel9202 wants to merge 6 commits into
Seagey:mainfrom
vpatel9202:ha-mcp-http

Conversation

@vpatel9202

Copy link
Copy Markdown
Contributor

Part of #351 (1 of 3 for the Home Assistant app). Independent of the other PRs.

COUCH_MCP_HTTP=1 runs bridge/mcp_server.py as Streamable HTTP on 127.0.0.1 (port COUCH_MCP_PORT, default 5401) for Home Assistant's MCP integration. Without it, stdio mode is unchanged: same tool names, same behaviour for /api/ask.

  • Loopback bind with FastMCP's DNS-rebinding guard. No sign-in, because Home Assistant's MCP client can't send a plain token. Server name "Couch TV", stateless sessions.
  • Tool set for voice agents: tv_ prefix, no screenshot (results reach the model as JSON text) or uninstall_app. Power off (tv_power, the power and sleep keys, off_after_episode) is refused unless COUCH_MCP_ALLOW_POWER_OFF=1. Read-only tools carry readOnlyHint.
  • Home Assistant drops a call after 5 s of silence (httpx's default read timeout) and after 30 s in all. Tools send a log keep-alive every 2 s and answer pending after COUCH_MCP_WAIT (24 s) while the work finishes in the background.
  • The instructions and docstrings no longer name a room or a city.
  • Tests: tests/test_mcp_http.py (11), including a client with a 5 s read timeout against an 8 s tool.
  • Docs: a section in docs/AI-AGENT.md.

Tested against Home Assistant Core 2026.11.3 (its MCP client is mcp 2.3.0), and through Home Assistant's own client library: a 20 s tv_play answers, and a pending comes back past the wait.

🤖 Generated with Claude Code

vpatel9202 and others added 3 commits November 27, 2026 23:51
Same tools on a second FastMCP on 127.0.0.1, with a tv_ prefix, no screenshot
or uninstall, power off only when allowed, and a tool that takes over 24 s
answers pending and finishes in the background (Home Assistant gives a tool
30 s). The instructions and docstrings no longer name a room or a city.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…child, wait parsed only in HTTP, docs say 127.0.0.1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e it Couch TV, stateless

Home Assistant's client drops a response stream that is silent for 5 s, so a
slow tool (tv_play, about 15 s) looked like an error. A log line now goes down
the request's own stream every 2 s. Stateless, because Home Assistant opens a
new session for every call: no session to close, so no standalone SSE writer
traceback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Seagey

Seagey commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks, this is really tidy work. Stdio mode is unchanged, the Host and Origin checks held up, and the tests pass. Two things to fix before it can go in:

  1. Power off gets past the refusal. The check in bridge/mcp_server.py compares the raw key against "power" and "sleep", but resolve_key in bridge/tv.py also takes raw names, so KEYCODE_POWER and KEYCODE_SLEEP go through with COUCH_MCP_ALLOW_POWER_OFF unset (tried against a fake bridge). Checking the resolved key and adding a test for the KEYCODE_ forms should do it.
  2. tv_launch takes any intent:// link in HTTP mode, so a model could open the TV's own Settings and finish the job with taps. Could HTTP mode accept only app ids and package names?

Hold off on big changes until there's an answer on the wider question in #351, but these two are worth fixing either way.

vpatel9202 and others added 3 commits November 28, 2026 15:57
… raw name for off: KEYCODE_POWER, KEY_POWER, PowerOff, power_toggle), tv_launch takes no links

Review on Seagey#352. Raw key names still work with COUCH_MCP_ALLOW_POWER_OFF=1, and stdio mode is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…p keys through, raw key names stay refused

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…OW_SETTINGS=1 (the settings, bluetooth and network ids, each kind of TV's settings app, the settings key)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vpatel9202

Copy link
Copy Markdown
Contributor Author

Thanks for catching both. Fixed and pushed:

  1. Power off. The KEYCODE_ forms weren't the only way through: each kind of TV has its own raw name for off (KEY_POWER on Samsung, PowerOff on Roku, power_toggle on VIZIO, 1012 on TiVo). Rather than list them, HTTP mode now takes only the named keys in tv_key's description. COUCH_MCP_ALLOW_POWER_OFF=1 lets power and sleep through, and raw names stay refused either way. The tests try one raw off name per TV type.
  2. Links. tv_launch now refuses links in HTTP mode. App ids and package names alone still left Settings one call away (the settings id, com.android.tv.settings, the Settings key), so I added COUCH_MCP_ALLOW_SETTINGS, off by default, which refuses those, the bluetooth and network screens, and each TV type's settings app (taken from the settings entry in apps.py).

Honestly, it's a speed bump, not a wall. With arrow keys and OK, a model that tries hard can still reach Settings from the home screen, and nothing short of removing navigation stops that. What it stops is a misheard "open settings" and the quick way in, which is the realistic risk with a voice assistant. For the buttons that do the damage, I opened #360 separately: tap now refuses factory reset, clear data, uninstall and the like, however the model got there. It stands on its own, off main, if you want it whatever you decide on #351.

@Seagey

Seagey commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Thanks for fixing both, and for being upfront that it's a speed bump rather than a wall. I've merged #360 on its own. I haven't reviewed these new commits yet. This one is still waiting on the decision in #351.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants